/**
 * FileName:     Cpabe.java
 * @Description: TODO()
 * All rights Reserved, Designed By HangZhou YuanTiao LTD.
 * Copyright:    Copyright(C) 2013-2014
 * Company       HangZhou YuanTiao LTD.
 * @author:    Nanio
 * @version    V1.0
 * Createdate:         Nov 16, 2014 3:20:06 PM
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * Nov 16, 2014       Nanio          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package com.hznu.ucenter.func.main;

import com.hznu.ucenter.func.cpabe.AbeDecrypt;
import com.hznu.ucenter.func.cpabe.AbeEncrypt;
import com.hznu.ucenter.func.cpabe.AbeKengen;
import com.hznu.ucenter.func.cpabe.AbeSetup;
import com.hznu.ucenter.func.cpabe.object.InterValue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Cpabe {


	/**
	 *
	 * @Title: setup
	 * @Description: TODO()
	 * @param: @param pubfile
	 * @param: @param mkfile
	 * @return: void
	 * @throws
	 */
	public void setup(String pubfile, String mkfile){
		AbeSetup setup = new AbeSetup();
		setup.setup(pubfile, mkfile);
	}

	public void setup(InterValue value){
		AbeSetup setup = new AbeSetup();
		setup.setup(value);
	}


	/**
	 *
	 * @Title: keygen
	 * @Description: TODO()
	 * @param: @param pubfile
	 * @param: @param mkfile
	 * @param: @param attfile
	 * @param: @param prifile
	 * @param: @param attList
	 * @return: void
	 * @throws
	 */
	public void keygen(String pubfile, String mkfile, String attfile, String prifile, String attList){
		AbeKengen gen = new AbeKengen();
		gen.keyGen(pubfile, mkfile, attfile, prifile, attList);
	}

	public void keygen(InterValue value, String list){
		AbeKengen gen = new AbeKengen();
		gen.keyGen(value, list);
	}

	/**
	 *
	 * @Title: enc1
	 * @Description: TODO()
	 * @param: @param policy
	 * @param: @param pubfile
	 * @param: @param plaintext
	 * @param: @param outputfile
	 * @param: @return
	 * @return: String
	 * @throws
	 */
	public String encrypt(String policy, String pubfile, String plaintext, String outputfile){
		AbeEncrypt enc1 = new AbeEncrypt();
		String result  = enc1.encrypt(policy, pubfile, plaintext, outputfile);
		return result;
	}

	public String encrypt(InterValue value, String policy, String plaintext){
		AbeEncrypt enc1 = new AbeEncrypt();
		String result  = enc1.encrypt(value, policy, plaintext);
		return result;
	}

	/**
	 *
	 * @Title: dec1
	 * @Description: TODO()
	 * @param: @param pubfile
	 * @param: @param attfile
	 * @param: @param encfile
	 * @param: @param outfile
	 * @param: @return
	 * @return: String
	 * @throws
	 */
	public String decrypt(String pubfile, String attfile, String prifile, String encfile, String outfile){
		AbeDecrypt dec = new AbeDecrypt();
		String result = dec.dec(pubfile, attfile, prifile, encfile, outfile);
		return result;
	}

	public String decrypt(InterValue value){
		AbeDecrypt dec = new AbeDecrypt();
		String result = dec.dec(value);
		return result;
	}

	private void testFile(){
		Cpabe cpabe = new Cpabe();
		String listStr ="(mytech,a,b,c,a2)";
		//String listStr ="(aaa,aaabbb,ccc,ddd,eee,fff,hhh,iii)";
		cpabe.setup("pk.key", "mk.key");
		cpabe.keygen("pk.key", "mk.key","ak.key", "pri.key", listStr);
		cpabe.encrypt("(((((((mytech and a) and b) and c) or ((mymoney and a) and b)) or (mymakert and a)) or (mybank and a)) or (myshell and a))", "pk.key", "success!!", "encfile1");
		//cpabe.encrypt("(aaa and aaabbb and ccc and ddd and eee and fff and hhh and iii)", "pk.key", "success!!", "encfile1");
		//cpabe.encrypt("(mytech and a)", "pk.key", "success!!", "encfile1");

		String result = cpabe.decrypt("pk.key", "ak.key", "pri.key", "encfile1", "decfile");
	}

	private void testInterValue(){
		InterValue value = new InterValue();
		String attList = "(mytech,a,b,c)";
		String policy = "(((((((mytech and a) and b) and c) or ((mymoney and a) and b1)) or (mymakert and a)) or (mybank and a3)) or (myshell and a4))";
		String plainText = "success intervalue";
		setup(value);
		keygen(value, attList);
		encrypt(value, policy, plainText);
		if(decrypt(value) == null){
			System.out.printf("invalid policy");
		}
		value.printToString();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cpabe cpabe = new Cpabe();
//		cpabe.testFile();
//		cpabe.testInterValue();

		//rsu端加密信息模拟
		InterValue value = new InterValue();

		value.setAttKey("FwAAAChGQUVGNDUsYmx1ZSxMaW5jb2xuLEEpAAQAAACiYSwWRNGN/CBs8yGJktML6h8Tx0iZ01V3gYbe1ixF+GtMQdvijNtw9xVpgDzzWjoEIhRsppw3tQTRTQngadevKbC4L+xnbuHei5p8ctL/oa53ebqrAwNe48r0TKlEXBWEzG+CFRJu4z6awKr9x7pwDnFCWT4wh4aEOVTJQagSP4CyBvX6zmhwGXr03R4SrZqp18L6eBYkQkxyHTgE7SJ6st18bOX72tdiLJtE3S3XfUNzCXTfl796Bv+OzQxc1zMxUKGeSYoS2cHv9+2iwhHHvxxEw3porRd/bP8iHXzg7s8fifwM2oSc4q+cH73fesaCMtkH+cCcrK/qKqLMIRdfGqcaAPd4YO5aw3SsuRimkjnL+AIgiNwzpkgE2WSfEK50xVRh+rnHSFj99SsCmByS+Ogvn0rk8iSsl2LpRN2LmwqzIjRcs8PQ9kerVjVBxwCCsDwBNMtkR9ztkDznhQMkifaUI4Amkl76CuPJOIduk56kY715YZ+uLAGQYYOea2SN/Jw4I7vTCGyHPOtFcdf+Tq35O/HWpaft4b7SEYPcflss2VKoxFp+09RL/ydbG3uGUCs9F5ZO5RlVezLi7Yo4l/G0S9+BD4aN7huoJXEX32fk3t8pq/kmU8zvnDRfqwZMN4UbzFrIIsezRAyCjtAsQdAqMj10da0qOFnlM9kswkr048LIhwVGmp3oGnzZKoVD16K3MTuopzxkn6s/JZokSs09rMCKGFOJjcQonFd2ORXqzbk499n43G5Lb2wOylibYfFdCq+KM3XaRnp14lzNoIJZdX8Y3b1m+GEw7tnGsoeWJkqvOe9J5GvfdYD6JtsnvdQ7a3fXDVYsZdv0piTYlWR3wRizhRLZQqaY2JJr9kdkDwpsTtiQStUced8D/e+cJ+u+DyAasJmYliYrmcRRGJ3ELaLFoUaFitSL3db4/CP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KfRvUN+5vN50GdNhnIUB014NSx+xlIQ5fTlVPw5BpFt dadMZL6GKHd3zp/nV1bAEk0SrMoc7ktBynv6fOngCR581Hy4WlF5zFz9g/Dv a44Nmkc5d5NOoFNSPsox2n8cNftAFZIYXIpZ80k6v0h7IIh+9ENPSHfezm26 yjxERgIEiLR4Le4k5s1iz35FkTV4iB/VklEdG9muN5BsH03ACTW89ImbWFac cDJDVPx0OFrjMDTvau893ludrFLQ5FjiobUOoshVE9ur7uv7z6EyEqvUn47M GdRLv91ZvQYzE0AkrzlOvvGMkXKv7veBzUTbQ3WAaBNOzTx+deoVq31befdH uTDhW3Xe9VBJDX8p8vHAZrpx96W7ThiIsQs4xZttzUziPYji7pVoulRsAHxj 2RMcGzMfg507p42ZAKz7yyE5YrP9cQTGzS5KEXC+KQDHBxD0ONdThup1hZzk 0U7M2NxbDC92va926Ltw/qj3qdGmY/tdGwubw3cJtsAN7yCksthW+qx54l2H oUv4jalDUA3H2A7TSZpBOYaHqtUZ81vQs6ypO7geNZ9YmEi7iUJnIDqCWVL8 acVz3lMrKtYQ27gNE1XHz6PKjNehYkBjLA5/cz/4eLR7Nrz2xeDlY8rNNB+6 PdiBxk4UZYRHvrtdJnQ9p0jPOYCuR/FYYTWOwYb3EHeqWMZwX7zXm1gQqmGa D9pGY9feZ2xFws3uysB+5F0VoQxQtUtEtcl0syWJx6DtYsM=:ABE_CP_END:IV:avXz1JYmwVbYG2g99+EejC+HOkVkPppDGliqrfsU2TwfIgF9v5DK2WSYam4z GUQyeqhPX0v19CDXzKbe8y5fK6bmgPGQLpjDOdWchhDTCIfmBq7el3+6JYEl T6l0ssn9/FSGUF16Th0V9zT1kLGvkA5XnXDq9RuR8tW1MoJ92Q0=:IV_END:AES: :AES_END");
		cpabe.decrypt(value);


		System.out.println("**********************************************************************************************************");
		value.printToString();
		System.out.println("**********************************************************************************************************");

	}


	//===========================================================================
	public InterValue Myencryot(String plainText,String policy) {
		InterValue value = new InterValue();
		value = mysetup("pk.key", "mk.key");
		encrypt(value, policy, plainText);
		return value;
	}

	public InterValue mysetup(String pubfile, String mkfile) { /*生成主密钥存在mkfile中，公钥存在pubfile中*/
		InterValue value = new InterValue();
		String pk;
		String mk;
		pk = readFileByLines(pubfile);
		if (pk == null) {
			return null;
		}
		mk = readFileByLines(mkfile);
		if (mk == null) {
			return null;
		}
		value.setMasKey(mk);
		value.setPubKey(pk);
		return value;
	}

	public static String readFileByLines(String fileName) {
		File file = new File(fileName);
		String ans = "";
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			while ((tempString = reader.readLine()) != null) {
				ans+=tempString;
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			return ans;
		}
	}
	//==============================================================
	public InterValue Mykeygen(String attList) {
		InterValue value = new InterValue();
		value = mysetup("pk.key", "mk.key");
		keygen(value, attList);
		value.setMasKey("");
		return value;
	}

	public InterValue Mydecrypt(String vehicle_attkey, String vehicle_prikey, String vehicle_encrypt) {
		InterValue value= new InterValue();
		value.setAttKey(vehicle_attkey);
		value.setPriKey(vehicle_prikey);
		value.setPubKey("A9uY5IrgM6a1CNbz0ptueHcv0GvxlPtfPdSMOewKc+5TsoUTUJdiul57tL05H25niqp6w6u5s13M31LtGX6d0UJ0oY6bUCslw7THiQXDrKF2Pscxo7b0CM5p4r8SiH1XYhFAPv8ZX189aU6SI7FIBDejCAmnakhHjofv3fAP5vcEqXkbVL5C9EaeyIkZF5LmkXQfoSGlpcLQiDDlAF4s5hEC3zCdtYtnR0eWUTxQ/fPrtMTOaHjBA0Fe56ngOw1qdMxTUeXWgkhMfX2twus4QV7F1tKLw9AbAoHDtqxDBxRY3SQEpVszjEPk5epEZc0pOIgPHNwZrrGaJ7maO09r6JHWrdGpn+4UPY2DF7uYOdOobB8t6DVI7U2iKVJ389ahjZ6LMasDHq+J382BRUggDqV842X5L+14+/F00F0K4nIPtBRp0w4D9JW2KaHYrKNY+iUArFM6ONlrSU158U4hQSCizGJJZj7RkWIKgzWe23nY7kxZZlyDsvSZzfIA1O3hgGnz7q5M6PYm6QvRI1ZUoExQgK/Rdm+MSXEbdvEl1iA051NXWK2RSBZFCoAKHoq3HQXRGE+1UmCMIO1+Ex0LYptkXm9B8AiLodKX8u6kQVVLLgFuZ2bUN1o3TeFN5CYy7LmUpeL2FZ7GsMm+ShrbQlaCx+WAmk08SFag5xtgBrxcQrD9iLS0YoSUlS7+Sjv90sR6t4nLy59rc/ui6Z/HoLJBZ1z2HCJr0kAIA7s2oeA4oA0fYQKjzaG4y0HA9NgVp2pikSxvU8rV1mO+dOXFRUnir8f93Z59dcI2UA8yUGK5RFJYpJvH454oWjIz8etmn0TKI5WaIrXKjPIlZfldNQ==");
		value.setEncrypted(vehicle_encrypt);
		decrypt(value);
		return value;
	}

}
